//
//  ViewController.m
//  Work
//
//  Created by 娇娇 on 15/10/23.
//  Copyright (c) 2015年 work. All rights reserved.
//

#import "ViewController.h"
#import "ZHPickView.h"
#import "DemoViewController.h"
#import "LoginViewController.h"
#import "RegistViewController.h"
#import "FindPwdViewController.h"
#import "Reachability.h"
#import "Database.h"
#import "SVProgressHUD.h"
@interface ViewController ()<UIWebViewDelegate,ZHPickViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *login;
- (IBAction)regist:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwd;
@property (weak, nonatomic) IBOutlet UIButton *lizi;
@property (nonatomic,strong)ZHPickView *pickV;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *lanuch;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ViewController
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}
#pragma mark - 忘记密码
- (IBAction)forgetPwd:(id)sender {
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:
            // 没有网络连接
        {
            NSLog(@"NotReachable");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"请设置网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            
            [alert show];
            
        }
            
            break;
        case ReachableViaWiFi:
        {
            FindPwdViewController *findVC = [[FindPwdViewController alloc] init];
            
            [self.navigationController pushViewController:findVC animated:YES];
            
        }
            break;
        case ReachableViaWWAN:
        {
            
            
            
            FindPwdViewController *findVC = [[FindPwdViewController alloc] init];
            
            [self.navigationController pushViewController:findVC animated:YES];
        }
            break;
        default:
            break;
    }

    FindPwdViewController *findVC = [[FindPwdViewController alloc] init];
    
    [self.navigationController pushViewController:findVC animated:YES];
    
}
#pragma mark - 示例
- (IBAction)model:(id)sender {
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:
            // 没有网络连接
        {
            NSLog(@"NotReachable");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"请设置网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            
            [alert show];
            
        }
            
            break;
        case ReachableViaWiFi:
        {
            
            
            DemoViewController *demoVc = [[DemoViewController alloc] init];
            
            [self.navigationController pushViewController:demoVc animated:YES];
            

            
        }
            break;
        case ReachableViaWWAN:
        {
            
            
            
            DemoViewController *demoVc = [[DemoViewController alloc] init];
            
            [self.navigationController pushViewController:demoVc animated:YES];
            

        }
            break;
        default:
            break;
    }

    DemoViewController *demoVc = [[DemoViewController alloc] init];
    
    [self.navigationController pushViewController:demoVc animated:YES];
  
}
#pragma mark - 登录
- (IBAction)login:(id)sender {
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:
            // 没有网络连接
        {
            NSLog(@"NotReachable");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"请设置网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            
            [alert show];
            
        }
            
            break;
        case ReachableViaWiFi:
        {
            
            if (self.nameTextFiled.text.length == 0) {
                [SVProgressHUD showErrorWithStatus:@"请填写用户名"];
                return;
            }
            if (self.pwdTextFiled.text.length == 0) {
                [SVProgressHUD showErrorWithStatus:@"请输入密码"];
                return;
            }
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            loginVC.name = self.nameTextFiled.text;
            loginVC.pwd = self.pwdTextFiled.text;
            [self.navigationController pushViewController:loginVC animated:YES];
            
        }
            break;
        case ReachableViaWWAN:
        {
            
            if (self.nameTextFiled.text.length == 0) {
                [SVProgressHUD showErrorWithStatus:@"请填写用户名"];
                return;
            }
            if (self.pwdTextFiled.text.length == 0) {
                [SVProgressHUD showErrorWithStatus:@"请输入密码"];
                return;
            }

            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            loginVC.name = self.nameTextFiled.text;
            loginVC.pwd = self.pwdTextFiled.text;

            [self.navigationController pushViewController:loginVC animated:YES];
        }
            break;
        default:
            break;
    }

    
}
#pragma mark - 选择语言
- (IBAction)selectLauch:(id)sender {
    _pickV = [[ZHPickView alloc] initPickviewWithArray:self.dataArray isHaveNavControler:NO];
    [_pickV setPickViewColer:[UIColor whiteColor]];
    _pickV.delegate = self;
    [_pickV show];
}
- (void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString withString:(NSString *)str {
    self.lanuch.title = resultString;

}
#pragma mark - textFieldDelegate 
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.frame.size.height+textField.frame.origin.y > self.view.frame.size.height-260) {
        [UIView animateWithDuration:0.25f animations:^{
            
            self.view.frame = CGRectMake(0, self.view.frame.size.height-260-textField.frame.size.height-textField.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        }];
        
    }
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25f animations:^{
        self.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
    }];
    return YES;
}
#pragma mark - 点击空白处回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.nameTextFiled.delegate = self;
    self.pwdTextFiled.delegate = self;
    [self.login.layer setBorderWidth:1.0]; //边框宽度
    [self.lizi.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1,1, 1, 1 });
    self.login.layer.borderColor = colorref;
   
    self.lizi.layer.borderColor = colorref;
    self.dataArray = @[@"deutsch",@"global",@"Fran?ais",@"Português",@"Svenska",@"русский",@"한국어",@"日本語",@"In Italiano",@"De Nederlandse"].mutableCopy;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 注册
- (IBAction)regist:(id)sender {
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            // 没有网络连接
        {
            NSLog(@"NotReachable");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"请设置网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            
            [alert show];
            
        }
            
            break;
         case ReachableViaWiFi:
        {
            RegistViewController *registVC = [[RegistViewController alloc] init];
   
            [self.navigationController pushViewController:registVC animated:YES];
        
        }
            break;
            case ReachableViaWWAN:
        {
        
            RegistViewController *registVC = [[RegistViewController alloc] init];
            
            [self.navigationController pushViewController:registVC animated:YES];
        }
            break;
            default:
            break;
    }
   
    
}


/*
 self.navigationItem.title = @"登录";
 UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 NSURL *url = [[NSURL alloc] initWithString:@"http://www.sunways-portal.com/phone/login.jsp"];
 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
 webView.scrollView.showsVerticalScrollIndicator = NO;
 webView.scalesPageToFit = YES;
 webView.delegate = self;
 webView.scrollView.scrollEnabled = NO;
 [webView loadRequest:request];
 [self.view addSubview:webView];
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
 CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
 btn.layer.borderColor = colorref;

 */

/*
 -(void)webViewDidFinishLoad:(UIWebView *)webView
 {
 NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width];
 [webView stringByEvaluatingJavaScriptFromString:meta];
 }
 */
//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    //设置图片缩放
//    NSString* resizeImg = [NSString stringWithFormat:@"var script = document.createElement('script');"
//                           "script.type = 'text/javascript';"
//                           "script.text = \"function ResizeImages() { "
//                           "var myimg,oldwidth;"
//                           "var maxwidth = %f;" // UIWebView中显示的图片宽度
//                           "for(i=0;i <document.images.length;i++){"
//                           "myimg = document.images[i];"
//                           "if(myimg.width > maxwidth){"
//                           "oldwidth = myimg.width;"
//                           "myimg.width = maxwidth;"
//                           "}"
//                           "}"
//                           "}\";"
//                           "document.getElementsByTagName('head')[0].appendChild(script);",[UIScreen mainScreen].bounds.size.width-20];
//    [webView stringByEvaluatingJavaScriptFromString:resizeImg];
//    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
//    //设置页面缩放
//    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=self.view.frame.size.width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\""];
//    [webView stringByEvaluatingJavaScriptFromString:meta];
//    //设置字体大小
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
//    //字体颜色
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#5f5f5f'"];
//
//    //获取页面高度（像素）
//    NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
//    float clientheight = [clientheight_str floatValue];
//    //设置到WebView上
//    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, self.view.frame.size.width, clientheight);
//    //获取WebView最佳尺寸（点）
//    CGSize frame = [webView sizeThatFits:webView.frame.size];
//    //获取内容实际高度（像素）
//    NSString * height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))  + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];
//   float height = [height_str floatValue];
//    //内容实际高度（像素）* 点和像素的比
//    //这样换算会有问题，所以先注释
//    height = height * frame.height / clientheight;
//    //再次设置WebView高度（点）
//    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, self.view.frame.size.width, height);
//    //修正外部控件高度
////    self.superView.height = CGRectGetMaxY(webView.frame);
////    [self.superView setContentSize:CGSizeMake(DLScreenWidth, CGRectGetMaxY(self.view.frame) + 44)];
//    NSLog(@"webView frame %@",NSStringFromCGRect(webView.frame));
////    NSLog(@"introView frame %@",NSStringFromCGRect(self.introduceView.frame));
////    [webView.layer setBorderColor:[[UIColor clearColor] CGColor]];
//
//}


@end
